﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RapidSalesAdminPortalAPI.DataAccess;
using RapidSalesAdminPortalAPI.Models;
using WebAPI.Common;
using WebAPI.Common.Attributes;
using WebAPI.Controllers;
using WebAPI.Models;
using WebAPI.Util;

namespace RapidSalesAdminPortalAPI.Controllers
{
    //[Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class MultiLangSetController : ParentController
    {
        private readonly RS_AdmPortalContext _context;
        private readonly IConfiguration _configuration;
        private readonly ILogUtility _logUtility;
        private readonly ILogger _logger;
        private readonly IDataProtectorUtility _dataProtectorUntility;
        public MultiLangSetController(RS_AdmPortalContext context, IConfiguration configuration, ILogUtility logUtility, ILogger<FrIpMasterDatumController> logger, IDataProtectorUtility dataProtectorUntility)
        {
            _context = context;
            _configuration = configuration;
            _logUtility = logUtility; // sample code for DI of common utility class
            _logger = logger;
            _dataProtectorUntility = dataProtectorUntility;
        }

        [HttpPost("MultiLangSetList")]
        public async Task<ActionResult<ResultMsg>> MultiLangSetList(MultiLangSetListRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            IQueryable<FrIpMultiLangSet> paComont = _context.FrIpMultiLangSets.AsQueryable();
            paComont = paComont.Where(x => x.LangType == requestModel.lang);
            var paData = paComont.ToList().Select(x => new MultiLangSetListResponse()
            {
                Id = x.Id,
                Code = x.Code,
                Value = x.Value,
                Description = x.Description,
                Enable = x.Enable,
                LangType = x.LangType,

            }).AsQueryable();

            paData = DBHelper.DataSorting(paData, "Code", "asc");

            return resultMsg.SetResultMsg(
                 (int)ResultCode.Success,
                 HelperUtility.GetDescription(ResultCode.Success),
                 paData);

        }

        [HttpGet("{id}")]
        public async Task<ActionResult<ResultMsg>> Detail(Guid id)
        {
            var resultMsg = new ResultMsg();
            var dataDictionary = await _context.FrIpMultiLangSets.FindAsync(id);

            if (dataDictionary == null)
            {
                return resultMsg.SetResultMsg(
                (int)ResultCode.NoDataFound,
                HelperUtility.GetDescription(ResultCode.NoDataFound),
                null);
            }

            return resultMsg.SetResultMsg(
                (int)ResultCode.Success,
                HelperUtility.GetDescription(ResultCode.Success),
                new MultiLangSetListResponse()
                {
                    Id = dataDictionary.Id,
                    Code = dataDictionary.Code,
                    Value = dataDictionary.Value,
                    Enable = dataDictionary.Enable,
                    Description = dataDictionary.Description,
                    LangType = dataDictionary.LangType
                });
        }

        [Log("MultiLangSet", "Insert", description: "MultiLangSet/Insert", LogType: Enums.LogType.AdministratorAccountLog)]
        [HttpPost("Insert")]
        public async Task<ActionResult<ResultMsg>> Insert([FromBody] InsertRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            try
            {
                if (IsExists(requestModel.Code, requestModel.LangType))
                {
                    return resultMsg.SetResultMsg(
                   (int)ResultCode.DataAlreadyExists,
                   HelperUtility.GetDescription(ResultCode.DataAlreadyExists),
                   null);
                }

                FrIpMultiLangSet data = new FrIpMultiLangSet();
                data.Id = Guid.NewGuid();
                data.Code = requestModel.Code;
                data.Value = requestModel.Value;
                data.LangType = requestModel.LangType;
                data.Description = requestModel.Description;
                data.Enable = requestModel.Enable;
                _context.Entry(data).State = EntityState.Added;
                await _context.SaveChangesAsync();

                return resultMsg.SetResultMsg(
                (int)ResultCode.Success,
                HelperUtility.GetDescription(ResultCode.Success),
                null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return resultMsg.SetResultMsg(
                 (int)ResultCode.ErrorOnRequestUnMarshalling,
                 HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                 null);
            }
        }

        [Log("MultiLangSet", "Update", description: "MultiLangSet/Update", LogType: Enums.LogType.AdministratorAccountLog)]
        [HttpPost("Update")]
        public async Task<ActionResult<ResultMsg>> Update([FromBody] UpdateRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            try
            {
                var data = await _context.FrIpMultiLangSets.FirstOrDefaultAsync(x => x.Id == requestModel.Id);
                if (data == null)
                {
                    return resultMsg.SetResultMsg(
                        (int)ResultCode.NoDataFound, 
                        HelperUtility.GetDescription(ResultCode.NoDataFound), 
                        null);
                }

                //if (IsExists(requestModel.Code, requestModel.LangType))
                //{
                //    return resultMsg.SetResultMsg(
                //   (int)ResultCode.DataAlreadyExists,
                //   HelperUtility.GetDescription(ResultCode.DataAlreadyExists),
                //   null);
                //}

                data.Value = requestModel.Value;
                data.LangType = requestModel.LangType;
                data.Enable = requestModel.Enable;
                data.Description = requestModel.Description;
                data.Enable = requestModel.Enable;
                _context.Entry(data).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return resultMsg.SetResultMsg(
                (int)ResultCode.Success,
                HelperUtility.GetDescription(ResultCode.Success),
                null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return resultMsg.SetResultMsg(
                 (int)ResultCode.ErrorOnRequestUnMarshalling,
                 HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                 null);
            }
        }


        [NonAction]
        public bool IsExists(string code, int langtype)
        {
            if (string.IsNullOrWhiteSpace(code))
                return false;
            return _context.FrIpMultiLangSets.Any(x => x.Code.ToLower() == code.ToLower() && x.LangType == langtype);
        }

    }
}
